<?php
namespace app\admin\controller;
use think\Db;
class Expand extends Base
{   
    //列表
    public function lists(){
        if( is_post() ){
            $page     = input('page');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            
            $sql = "
                select
                    [1]
                from
                    expand
                where 
                    1 = ?
                ";
            $param = [1];
            if ( !empty($keyword) ) {
                $sql .= " and (t1 like ? or t2 like ? or t3 like ? or t4 like ?)";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and create_time <= ?";
                $param[] = strtotime($max_time);
            }
            #分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
            $page_data   = com_page_data($total, $page,  $page_size);
            
            #列表
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by id desc limit $start, $page_size";
            $field       = "*";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                
                $list[$k]['action']         =  com_page(['ico'=>'edit','title'=>'编辑扩展','url'=>url('expand/edit','id='.$v['id'])]);
                $list[$k]['action']        .=  com_page(['ico'=>'del3','title'=>'删除','url'=>url('c/del'), 'post'=>['id'=>$v['id'],'t'=>'expand'],'confirm'=>"你确定要删除[{$v['note']}]吗"]);
            }
            $com_body    = com_build_table($list, array("checkbox", "id",  "uuid", "note",  "t1", "t2","t3","t4","create_time", "action"));
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data]);
        }
       	//模板赋值
        $data = [
            'nav'       => ['程序扩展', '列表'],
        ];
        return view('', $data);
    }
    
    //添加
    public function add(){
        if( is_post() ){
            $data = com_data(input());
            $rule = [
                    'uuid'         =>  'require|unique:expand',
                ];
            $msg = [
                   'uuid.require'  =>  '请填写程序标识',
                   'uuid.unique'   =>  '标识重复',
                ];
            com_validate( $rule, $msg, $data, true );
            $data['create_time'] = time();
            Db::name('expand')->insert($data);
            $config = [
                'suc' => [
                    'msg' => '添加成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        } 
        return view();
    }
    
    
    //编辑
    public function edit( $id ){
        if( is_post() ){
            $d = com_data(input());
            $rule = [
                    'uuid'  => 'require|unique:expand,uuid,'.$id,
                ];
            $msg = [
                   'uuid.require'      =>  '请填写程序标识',
                   'uuid.unique'       =>  '标识重复',
                ];
            com_validate( $rule, $msg, $d, true );

            $d['id'] = $id;
            $d['update_time'] = time();
            Db::name('expand')->update($d);
            $config = [
                'suc' => [
                    'msg' => '修改成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        } 
        return view('',['info'=>Db::name('expand')->find($id)]);
    }
}